#!/bin/bash

#------------------------------------------------------------------------------
#Filename:      74802-sudouser.sh
#Version:       1.1
#Date:          2013/07/30
#Author:        yuanhui.shi
#Email:         yuanhui.shi@cs2c.com.cn
#Summary:       02.01系统核心功能-06基础命令行工具-11sudo程序-03添加用户到sudo用户文件
#Notes:         通过添加用户到sudo文件，赋予该用户root权限
#Copyright:     China Standard Software Co.,Ltd.
#History:
#               Version 1.0,2013/07/30
#               -The first one
#               Version 1.1,2013/09/09
#               -Modify the script to satisfy the shell standard
#------------------------------------------------------------------------------
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../lib/Echo.sh
source ./lib/Ssh.sh
source ../../lib/XmlParse.sh

function CleanData ()
{
  rm -rf /testdir
  rm -rf /tmp/tmp.result1
  rm -rf /etc/sudoers
  mv /etc/sudoers.bak /etc/sudoers

  userdel -r ${USER}

  EchoInfo "74802-sudouser.sh执行完毕"
}

trap "CleanData" EXIT

DATA_DIR="./data"
DATA_FILE="74802-sudouser.xml"

XmlParse ${DATA_DIR}/${DATA_FILE}

GetValue USER USER
GetValue PASSWORD PASSWORD

EchoInfo "添加普通用户$USER"
Adduser ${USER} ${PASSWORD}

EchoInfo "添加普通用户$USER到sudo文件"
cp /etc/sudoers /etc/sudoers.bak
sed -i -e 's/^Defaults *requiretty/#&/g' /etc/sudoers
echo "%test ALL= (ALL) NOPASSWD:ALL">> /etc/sudoers


EchoInfo "尝试在根目录/下创建目录testdir，使用普通命令"
rm -rf /testdir
Ssh ${USER} "127.0.0.1" ${PASSWORD} "LANG=C mkdir /testdir &>/tmp/tmp.result1"

#test ! -z "`grep "权限不够" /tmp/tmp.result1`" 
#update --hhj
grep "Permission denied" /tmp/tmp.result1
EchoResult "没有使用sudo时，没有权限新建文件/testdir" 

EchoInfo "尝试在/目录下创建目录，使用sudo命令"
[ -d /testdir ] && rm -rf /testdir
#Ssh ${USER} "127.0.0.1" ${PASSWORD} "sudo mkdir /testdir >/tmp/tmp.result1 2>&1"

su - ${USER} -c "LANG=C sudo mkdir /testdir >/tmp/tmp.result1 2>&1"

#test -z "`grep "权限不够" /tmp/tmp.result1`" 
#update --hhj
[ -d /testdir ]
EchoResult "使用sudo时，可以新建目录/testdir" 


